package world.xuewei.service.impl;

import world.xuewei.model.entity.Classify;
import world.xuewei.model.entity.Commodity;
import world.xuewei.model.entity.MerchantInfo;
import world.xuewei.dao.MerchantInfoDao;
import world.xuewei.model.entity.R;
import world.xuewei.service.MerchantInfoService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商户信息表(MerchantInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-11-15 19:27:49
 */
@Service("merchantInfoService")
public class MerchantInfoServiceImpl implements MerchantInfoService {
    @Resource
    private MerchantInfoDao merchantInfoDao;

    @Override
    public MerchantInfo queryByUserId(Long userId) {
        return merchantInfoDao.queryByUserId(userId);
    }


    @Override
    public MerchantInfo getRandomMerchant() {
        return merchantInfoDao.getRandomMerchant();
    }

    // 获取商户上架的所有商品
    @Override
    public List<Commodity> getPutAway(Long userId) {
        return merchantInfoDao.getPutAway(userId);
    }
    /**
     * 通过ID查询单条数据
     *
     * @return 实例对象
     */
    @Override
    public MerchantInfo queryById(Long userid) {
        return merchantInfoDao.queryById(userid);
    }


    @Override
    public List<Classify> getMerchantClassifies(Long merchantId) {
        return merchantInfoDao.getMerchantClassifies(merchantId);
    }

    @Override
    public List<Commodity> getMerchantCommodities(Long merchantId) {
        return merchantInfoDao.getMerchantCommodities(merchantId);
    }

    //获取商户的下架商品
    @Override
    public List<Commodity> getMerchantCommoditiesByStatus(long merchantId){

        return merchantInfoDao.getMerchantCommoditiesByStatus(merchantId);

    }


    /**
     * 分页查询
     *
     * @param merchantInfo 筛选条件
     * @param pageRequest  分页对象
     * @return 查询结果
     */
    @Override
    public Page<MerchantInfo> queryByPage(MerchantInfo merchantInfo, PageRequest pageRequest) {
        long total = this.merchantInfoDao.count(merchantInfo);
        return new PageImpl<>(this.merchantInfoDao.queryAllByLimit(merchantInfo, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param merchantInfo 实例对象
     * @return 实例对象
     */
    @Override
    public MerchantInfo insert(MerchantInfo merchantInfo) {
        this.merchantInfoDao.insert(merchantInfo);
        return merchantInfo;
    }

    /**
     * 修改数据
     *
     * @param merchantInfo 实例对象
     * @return 实例对象
     */
    @Override
    public Object update(MerchantInfo merchantInfo) {

        this.merchantInfoDao.update(merchantInfo);

        return R.ok().message("修改成功").data(merchantInfo);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.merchantInfoDao.deleteById(id) > 0;
    }
}
